unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  StdCtrls, IdException, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze, OleCtnrs,
  uDebug, AppEvnts, NativeXml;

type
  TMainForm = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    Button1: TButton;
    Button2: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    MemoResponse: TMemo;
    MemoRequest: TMemo;
    EditHost: TEdit;
    IdTCPClient1: TIdTCPClient;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    ComboBoxCommand: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    IdAntiFreeze1: TIdAntiFreeze;
    RadioGroup1: TRadioGroup;
    OleContainer1: TOleContainer;
    Button3: TButton;
    ApplicationEvents1: TApplicationEvents;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ComboBoxCommandChange(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  MainForm: TMainForm;
  S: string;

implementation

{$R *.dfm}

uses uStringUtils;

procedure TMainForm.Button1Click(Sender: TObject);
var
  i: integer;
  Fs: TFileStream;
  OleData: TFileStream;
  Data: TMemoryStream;
  DataLength: integer;
begin
try
  MemoResponse.Lines.Clear;
  with IdTCPClient1 do
    begin
    Host := EditHost.Text;
    Port := 3030;
    try
      Connect;
      try
        Socket.Writeln(UTF8Encode(ComboBoxCommand.Text));
        if Label3.Caption <> '' then
          begin
          try
            Fs := TFileStream.Create(Label3.Caption, fmOpenRead);
            Socket.Writeln(UTF8Encode('Content-size=' + IntToStr(Fs.Size)));
            for i := 0 to MemoRequest.Lines.Count - 1 do
              Socket.Writeln(UTF8Encode(MemoRequest.Lines.Strings[i]));
            Socket.Write(Fs, Fs.Size);
          finally
            fs.Free;
          end;
          end
        else
         for i := 0 to MemoRequest.Lines.Count - 1 do
            Socket.Writeln(UTF8Encode(MemoRequest.Lines.Strings[i]));

        S := '';
        Caption := 'Waiting...';
        DataLength := 0;
        while S <> '.' do
          begin
          S := UTF8Decode(Socket.Readln);
          if Pos('Content-length=', S) <> 0  then
            begin
            DataLength := StrToInt(Copy(S, Pos('=', S) + 1, Length(S) - Pos('=', S)));
            ShowMessage('Binding data present!');
            end;
          MemoResponse.Lines.Add(S);
          end;
     if DataLength <> 0 then
       begin
       Data := TMemoryStream.Create;
       IdTCPClient1.Socket.ReadStream(Data, DataLength);
       if SaveDialog1.Execute and (SaveDialog1.FileName <> '') then
         begin
         OleData := TFileStream.Create(SaveDialog1.FileName, fmCreate);
         Data.Position := 0;
         OleData.CopyFrom(Data, Data.Size);
         OleData.Free;
         end;
       Data.Free;  
       end;
       Caption := 'Simple text client';
    except
      on E: Exception do gtSaveException(E);
    end;
 finally
   Disconnect;
 end;
 end;
except
  on E: Exception do gtSaveException(E);
end;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
if Label3.Caption=''
then begin
     if OpenDialog1.Execute
     then Label3.Caption:=OpenDialog1.FileName;
     end
else Label3.Caption:='';
end;

procedure TMainForm.ComboBoxCommandChange(Sender: TObject);
begin
 MemoRequest.Clear;
 MemoRequest.Lines.Add('Login=');
 MemoRequest.Lines.Add('Password=');
 case RadioGroup1.ItemIndex of
 0:begin
     case ComboBoxCommand.ItemIndex of
     1: begin //BREAK
        MemoRequest.Lines.Add('Lifetime=');
        end;
     2: begin //ENDTEST
        MemoRequest.Lines.Add('Lifetime=');
        end;
     3: begin //MISS
        MemoRequest.Lines.Add('Lifetime=');
        end;
     4: begin //NEWTEST
        MemoRequest.Lines.Add('Test-name=');
        end;
     end;
   end;
 1:begin end;
 2:
 case ComboBoxCommand.ItemIndex of
 0: begin //ADDGROUP
    MemoRequest.Lines.Add('Group-name=');
    end;
 1: begin //ADDSTUD
    MemoRequest.Lines.Add('Student-login=');
    MemoRequest.Lines.Add('Student-password=');
    MemoRequest.Lines.Add('Student-name=');
    MemoRequest.Lines.Add('Student-surname=');
    end;
 2: begin //ADDTEACH
    MemoRequest.Lines.Add('Teacher-login=');
    MemoRequest.Lines.Add('Teacher-password=');
    MemoRequest.Lines.Add('Teacher-name=');
    MemoRequest.Lines.Add('Teacher-surname=');
    MemoRequest.Lines.Add('Teacher-pulpit=');
    MemoRequest.Lines.Add('Teacher-job=');
    end;
 3: begin // ADDJOUR
    MemoRequest.Lines.Add('Student-login=');
    MemoRequest.Lines.Add('Group-name=');
    end;
 4: begin //DELGROUP
    MemoRequest.Lines.Add('Group-name=');
    end;
 5: begin //DELSTUD
    MemoRequest.Lines.Add('Student-login=');
    end;
 6: begin //DELTEACH
    MemoRequest.Lines.Add('Teacher-login=');
    end;
 7: begin //DELJOUR
    MemoRequest.Lines.Add('Student-login=');
    MemoRequest.Lines.Add('Group-name=');
    end;
 8: begin //EDITGROUP
    MemoRequest.Lines.Add('Group-name=');
    MemoRequest.Lines.Add('Group-newname=');
    MemoRequest.Lines.Add('Group-newdescription=');
    end;
 9: begin //EDITSTUD
    MemoRequest.Lines.Add('Student-login=');
    MemoRequest.Lines.Add('Student-newlogin=');
    MemoRequest.Lines.Add('Student-newpassword=');
    MemoRequest.Lines.Add('Student-newname=');
    MemoRequest.Lines.Add('Student-newsurname=');
    end;
 10: begin //EDITTEACH
     MemoRequest.Lines.Add('Teacher-login=');
     MemoRequest.Lines.Add('Teacher-newlogin=');
     MemoRequest.Lines.Add('Teacher-newpassword=');
     MemoRequest.Lines.Add('Teacher-newname=');
     MemoRequest.Lines.Add('Teacher-newsurname=');
     MemoRequest.Lines.Add('Teacher-newpulpit=');
     MemoRequest.Lines.Add('Teacher-newjob=');
     end;
 11: begin //EDITPASW
     MemoRequest.Lines.Add('NewPassword=');
     end;
 12: begin //GETGROUP
     MemoRequest.Lines.Add('Group-name=');
     end;
 13: begin //GETSTUD
     MemoRequest.Lines.Add('Student-login=');
     MemoRequest.Lines.Add('Group-name=');
     end;
 14: begin //GETTEACH
     MemoRequest.Lines.Add('Teacher-login=');
     end;
 15: ;//HELP
 16: ;//START
 17: ;//STOP
 end;
 end;
 MemoRequest.Lines.Add('.');
end;

procedure TMainForm.RadioGroup1Click(Sender: TObject);
begin
  ComboBoxCommand.Items.Clear;
  case RadioGroup1.ItemIndex of
  0: begin
     ComboBoxCommand.Items.Add('ANSWER');
     ComboBoxCommand.Items.Add('BREAK');
     ComboBoxCommand.Items.Add('ENDTEST');
     ComboBoxCommand.Items.Add('MISS');
     ComboBoxCommand.Items.Add('NEWTEST');
     ComboBoxCommand.Items.Add('NEXT');
     ComboBoxCommand.Items.Add('TESTINFO');
     end;
  1: begin
     ComboBoxCommand.Items.Add('TESTVALUE');
     ComboBoxCommand.Items.Add('TESTDELETE');
     ComboBoxCommand.Items.Add('TESTCHANGE');
     ComboBoxCommand.Items.Add('TESTLOAD');
     ComboBoxCommand.Items.Add('TESTUPLOAD');
     ComboBoxCommand.Items.Add('RESULTGET');
     ComboBoxCommand.Items.Add('RESULTDEL');
     ComboBoxCommand.Items.Add('ANSWERGET');
     ComboBoxCommand.Items.Add('GETSTUD');
     ComboBoxCommand.Items.Add('GETTEACH');
     ComboBoxCommand.Items.Add('GROUPEXAMINE');
     ComboBoxCommand.Items.Add('GETEXAMINE');
     end;
  2: begin
     ComboBoxCommand.Items.Add('ADDGROUP');
     ComboBoxCommand.Items.Add('ADDSTUD');
     ComboBoxCommand.Items.Add('ADDTEACH');
     ComboBoxCommand.Items.Add('ADDJOUR');
     ComboBoxCommand.Items.Add('DELGROUP');
     ComboBoxCommand.Items.Add('DELSTUD');
     ComboBoxCommand.Items.Add('DELTEACH');
     ComboBoxCommand.Items.Add('DELJOUR');
     ComboBoxCommand.Items.Add('EDITGROUP');
     ComboBoxCommand.Items.Add('EDITSTUD');
     ComboBoxCommand.Items.Add('EDITTEACH');
     ComboBoxCommand.Items.Add('EDITPASW');
     ComboBoxCommand.Items.Add('GETGROUP');
     ComboBoxCommand.Items.Add('HELP');
     ComboBoxCommand.Items.Add('START');
     ComboBoxCommand.Items.Add('STOP');
     end;
  end;
  ComboBoxCommand.ItemIndex := 0;
end;

procedure TMainForm.Button3Click(Sender: TObject);
var i: integer;
  Doc: TNativeXml;
  Node: TXmlNode;
begin
try
  i := 0;
  i := i div i;
except
  on E: Exception do gtSaveException(E);
end;
end;

end.
